<?php
namespace App\Http\Questionnaire\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;


class LabelController  extends Controller
{
    public function __construct(Request $request)
    {
        $token = getToken($request);
        $uid = Redis::get("code:" . $token);
        $this->uid = $uid;
    }

    public function add_label(Request $request){


        $data = $request->only("title","type","flid","qid");
        $data['uid']=$this->uid;
        //title不存在 格式为空白字符 长度少于2大与20
        if(!array_key_exists('title',$data)||!preg_match('/^[\S\s]{2,20}$/u',$data['title'])) return ThrowApiExc('32001');
        //type不存在 格式为非字数字 长度不等于1
        if(!array_key_exists('flid',$data)||!preg_match('/^[\d]{1,10}$/u',$data['flid'])) return ThrowApiExc('32029');
        //qid不存在 格式为非字数字 长度不等于1
        if(!array_key_exists('qid',$data)||!preg_match('/^[\d]{1,10}$/u',$data['qid'])) return ThrowApiExc('32031');

        $res = DB::table("Q_label")->insertGetId($data);

            if($res){
                DB::table("Q_label")->where("id",$res)->update(["orderId"=>$res]);
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }

    }

    public function update_label(Request $request,$qlid){
        $data = $request->only("title","status","flid","qid");
        $data['uid']=$this->uid;
        //title不存在 格式为空白字符 长度少于2大与20
        if(!array_key_exists('title',$data)||!preg_match('/^[\S\s]{2,20}$/u',$data['title'])) return ThrowApiExc('32001');
//        //type不存在 格式为非字数字 长度不等于1
//        if(!array_key_exists('status',$data)||!preg_match('/^[\d]{1,2}$/u',$data['type'])) return ThrowApiExc('32002');
        //type不存在 格式为非字数字 长度不等于1
        if(!array_key_exists('flid',$data)||!preg_match('/^[0-9]{0,10}$/u',$data['flid'])) return ThrowApiExc('32029');
        //qid不存在 格式为非字数字 长度不等于1
        if(!array_key_exists('qid',$data)||!preg_match('/^[\d]{1,10}$/u',$data['qid'])) return ThrowApiExc('32031');

        $res = DB::table("Q_label")->where("id",$qlid)->update($data);

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }

    }

    public function select_label(Request $request,$qid){
        $data= $request->only("title","status","flId");



        //title不存在 格式为空白字符 长度少于2大与20
        if(array_key_exists('title',$data)&&!preg_match('/^[\S\s]{2,20}$/u',$data['title'])) return ThrowApiExc('32001');

        if(!array_key_exists('status',$data)||!preg_match('/^[0-9]{1,10}$/',$data['status']))  return ThrowApiExc('42112');
        $where=[["qid",$qid],["status",1]];

        if(array_key_exists("flId",$data)){
            if($data['flId']==0){
                $where[]=["flId","!=",$data['flId']];
            }else{
                $where[]=["flId",$data['flId']];
            }

        }else{
            $where[]=["flId",0];
        }
        if(array_key_exists('title',$data)){
            $where[]=["title","like","%".$data['title']."%"];
        }



            $res = DB::table("Q_label")->where($where)->get();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }
    }


    public function delete_label($qlid){
        $res = DB::table("Q_label")->where("id",$qlid)->update(['status'=>0]);
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
              ];
    }


    public function add_label_content(Request $request,$qlid){
        $data = $request->only("content","level");
        $data['qlid']=$qlid;
        $data['uid']=$this->uid;
        $data['createdTime']=date("Y-m-d H:i:s",time());
        $data['updateTime']=date("Y-m-d H:i:s",time());
        $res = DB::table("Q_label_describe")->insertGetId($data);

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }

    }

    public function update_label_content(Request $request,$qldid){
        $data = $request->only("content","level","qlid");
        $data['updateTime']=date("Y-m-d H:i:s",time());
        $res = DB::table("Q_label_describe")->where("id",$qldid)->update($data);

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }
    }

    public function select_label_content(Request $request,$qlid){

        if(!preg_match('/^[\d]{1,10}$/u',$qlid)) return ThrowApiExc('32030');

        $label = DB::table("Q_label")->where("id",$qlid)->first();
        if($label['flid']==0){
            $inArr = DB::table("Q_label")->where("flid",$qlid)->get();

            $linshi = [];
            foreach ($inArr as $k=>$v){
                $linshi[]=$v['id'];
            }

            $res = DB::table("Q_label_describe as qld")
                ->select("qld.*","ql.title as qlTitle","ql.id as qlId","users.name as uname")
                ->leftJoin("Q_label as ql","ql.id","=","qld.qlid")
                ->leftJoin("users","users.id","=","qld.uid")
                ->whereIn("qld.qlid",$linshi)->orWhere("qld.qlid",$qlid)->get();
        }else{
            $res = DB::table("Q_label_describe as qld")->select("qld.*","ql.title as qlTitle","ql.id as qlId","users.name as uname")
                ->leftJoin("users","users.id","=","qld.uid")
                ->leftJoin("Q_label as ql","ql.id","=","qld.qlid")->where("qlid",$qlid)->get();
        }




        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '获取成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }

    }

    

    public function delete_label_content($qldid){
        $res = DB::table("Q_label_describe")->where("id",$qldid)->delete();

            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => $res
            ];

    }
    
    public function select_uq_content($quid){

        $data = DB::table("Q_q_p_users as qua")
            ->where("qua.id",$quid)->first();
//        $arr = preg_split("/([\s\n\<p\>]{3,99})/",$data['content'],-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        $arr=explode("|||||",$data['content']);


        foreach ($arr as $k=>$v){
                if($v==""){
                    unset($arr[$k]);
                }else{
                    $arr[$k]=[
                        "status"=>1,
                        "content"=>$v
                    ];
                }
                
        }


        if($arr){
            return [
                'status_code'      => 200,
                'message'     =>  '查找成功',
                'data' => [
                    "title"=>$data['title'],
                    "content"=>array_merge($arr)
                ]
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '查找失败 联系后端开发',
                'data' => $arr
            ];
        }
    }
    


    public function update_order(Request $request){
        $data = $request->only("lid","blid"); //lit 标签id blit 插入标签id

        $qdata = DB::table("Q_label")->where("id",$data['lid'])->fitst();
        $bdata = DB::table("Q_label")->where("id",$data['blid'])->fitst();
        if($qdata&&$bdata){

            DB::table("Q_label")->where("orderId","<=",$bdata['orderId'])->decrement("orderId");

            $res = DB::table("Q_label")->where("id",$data['lid'])->update([
                "orderId"=>$bdata['orderId']
            ]);

            if($res){
                return [
                    'status_code'      => 200,
                    'message'     =>  '修改成功',
                    'data' => $res
                ];
            }else{
                return [
                    'status_code'      => 503,
                    'message'     =>  '修改失败 并未修改数据',
                    'data' => $res
                ];
            }

        }else{
            return ThrowApiExc('32032');
        }
    }


}
